<?php
namespace app\admin\controller;

class Admin extends Base{

    public function index(){
        $isSuper=$this->getLoginUser()->is_super;
        $admin=model('Admin')->getNormalAdminsByStatus(['>',-1]);
        return $this->fetch('',[
            'admin'=>$admin,
            'isSuper'=>$isSuper]);
    }

    public function add(){

        if(request()->isPost()) {
            // 走插入逻辑
            $data = input('post.');
            // 严格校验提交的数据， tp5 validate 小伙伴自行完成，
            if (!empty($data['id'])){
                return $this->update($data);
            }
            $validate = validate('Admin');
            if(!$validate->scene('add')->check($data)) {
                $this->error($validate->getError());
            }
            $username=model('Admin')->get(['username' =>$data['username']]);
            if($username&&$username!=$data['username']){
                $this->error('用户名已存在');
            }
            $email=model('Admin')->get(['email' =>$data['email']]);
            if($email&&$email!=$data['email']){
                $this->error('电子邮箱已存在');
            }
            $data['code'] = mt_rand(100, 10000);
            $adminData = [
                'username' => $data['username'],
                'realname' => $data['realname'],
                'code' => $data['code'],
                'password' => md5($data['password'].$data['code']),
                'mobile' => $data['mobile'],
                'email' => $data['email'],
                'is_super' => 0, // 代表的是超级管理员
                'status' => 1,
            ];


            $adminId = model('Admin')->save($adminData);
            if($adminId) {
                $this->success('添加成功', url('admin/index'));
            }else {
                $this->error('添加失败');
            }

        }else {
            return $this->fetch('');
        }
    }

    public function edit($id){

        $admin=model('Admin')->get(intval($id));
        return $this->fetch('',['admin'=>$admin]);
    }

    public function update($data){

        if(empty($data['password'])){
            $username=model('Admin')->get(['username' =>$data['username'],'id'=>['<>',intval($data['id'])]]);
            if($username&&$username!=$data['username']){
                $this->error('用户名已存在');
            }
            $email=model('Admin')->get(['email' =>$data['email'],'id'=>['<>',intval($data['id'])]]);
            if($email&&$email!=$data['email']){
                $this->error('电子邮箱已存在');
            }
            $adminData = [
                'username' => $data['username'],
                'realname' => $data['realname'],
                'mobile' => $data['mobile'],
                'email' => $data['email'],
                'is_super' => 0, // 1 代表的是超级管理员
                'status' => 1,
            ];
            $adminId = model('Admin')->save($adminData,['id' =>intval($data['id'])]);
            if($adminId) {
                $this->success('更新成功', url('admin/index'));
            }else {
                $this->error('更新失败');
            }
        }else{
            $validate = validate('Admin');
            if(!$validate->scene('update')->check($data)) {
                $this->error($validate->getError());
            }
            $username=model('Admin')->get(['username' =>$data['username'],'id'=>['<>',intval($data['id'])]]);
            if($username&&$username!=$data['username']){
                $this->error('用户名已存在');
            }
            $email=model('Admin')->get(['email' =>$data['email'],'id'=>['<>',intval($data['id'])]]);
            if($email&&$email!=$data['email']){
                $this->error('电子邮箱已存在');
            }
            if ($data['password']!=$data['repassword']){
                $this->error('两次输入密码不一致');
            }
            $data['code'] = mt_rand(100, 10000);
            $adminData = [
                'username' => $data['username'],
                'realname' => $data['realname'],
                'code' => $data['code'],
                'password' => md5($data['password'].$data['code']),
                'mobile' => $data['mobile'],
                'email' => $data['email'],
                'is_super' => 0, // 代表的是超级管理员
                'status' => 1,
            ];
            $adminId = model('Admin')->save($adminData,['id' =>intval($data['id'])]);
            if($adminId) {
                $this->success('更新成功', url('admin/index'));
            }else {
                $this->error('更新失败');
            }
        }
    }

    public function stoplist(){
        $isSuper=$this->getLoginUser()->is_super;
        $admin=model('Admin')->getNormalAdminsByStatus(0);
        return $this->fetch('',[
            'admin'=>$admin,
            'isSuper'=>$isSuper]);
    }

    public function delist(){
        $isSuper=$this->getLoginUser()->is_super;
        $admin=model('Admin')->getNormalAdminsByStatus(-1);
        return $this->fetch('',[
            'admin'=>$admin,
            'isSuper'=>$isSuper]);
    }

    public function personal(){
        if(request()->isPost()){
            $data=input('post.');
            $username=model('Admin')->get(['username' =>$data['username'],'id'=>['<>',intval($data['id'])]]);
            if($username&&$username!=$data['username']){
                $this->error('用户名已存在');
            }
            $email=model('Admin')->get(['email' =>$data['email'],'id'=>['<>',intval($data['id'])]]);
            if($email&&$email!=$data['email']){
                $this->error('电子邮箱已存在');
            }
            $adminData = [
                'username' => $data['username'],
                'realname' => $data['realname'],
                'mobile' => $data['mobile'],
                'email' => $data['email'],
                'is_super' => 0, // 1 代表的是超级管理员
                'status' => 1,
            ];
            $adminId = model('Admin')->save($adminData,['id' =>intval($data['id'])]);
            if($adminId) {
                $this->success('更新成功', url('admin/personal'));
            }else {
                $this->error('更新失败');
            }
        }else{
            $adminId=$this->getLoginUser()->id;
            $admin=model('Admin')->get($adminId);
            return $this->fetch('',['admin'=>$admin]);
        }

    }

    public function repassword()
    {
        if (request()->isPost()){
            $data=input('post.');
            $validate = validate('Admin');
            if(!$validate->scene('repassword')->check($data)) {
                $this->error($validate->getError());
            }

            if ($data['password']!=$data['repassword']){
                $this->error('两次输入密码不一致');
            }
            $data['code'] = mt_rand(100, 10000);
            $accounData = [
                'code' => $data['code'],
                'password' => md5($data['password'].$data['code']),
            ];
            $accountId = model('Admin')->save($accounData,['id' =>intval($data['id'])]);
            if($accountId) {
                $this->success('更新成功');
            }else {
                $this->error('更新失败');
            }
        }else{
            $adminId=$this->getLoginUser()->id;
            $admin=model('Admin')->get($adminId);
            return $this->fetch('',['admin'=>$admin]);
        }

    }

}